import { Component, ViewChild, OnInit, EventEmitter, Input, Output, Host, SimpleChanges, ChangeDetectorRef } from '@angular/core';
import { UtilsService } from '../../../../shared/services/utils.service';
import { ResearchRiskRatingService } from '../researchRiskRating.service';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { AppComponent } from '../../../../app.component';
import { Subscription } from 'rxjs/Subscription';
import { ClusterComponent } from '../../riskCluster/cluster.component';
import { ClusterModelWrapper } from '../../riskCluster/cluster.model';
import { CompanyCountModel } from '../../../../shared/models/companyCount.model';
import { RiskFilterModel } from '../models/riskFilter.model';
import { RiskListModel } from '../models/riskList.model';
import { ColumnOptions } from '../../../../shared/components/table/models/columnOptions.model';
import { TableComponent, PaginationPositionDisplayEnum } from '../../../../shared/components/table/table.component';
import { TableOptions } from '../../../../shared/components/table/models/tableOptions.model';
import { PageOptions } from '../../../../shared/components/table/models/pageOptions.model';
import { TableRouterLink } from '../../../../shared/components/table/models/tableRouterLink.model';
import { ClusterService } from '../../riskCluster/cluster.service';

@Component({
    selector: 'list-risk-rating',
    templateUrl: './companyList.component.html',
    styleUrls: ['./companyList.component.scss', '../../../../../assets/table.scss'],
    providers: [UtilsService, ResearchRiskRatingService]
})

export class CompaniesListComponent extends ClusterComponent {
    public filter: RiskFilterModel = new RiskFilterModel();

    public riskClusters: ClusterModelWrapper[];
    public companies: RiskListModel[];
    private routePath: string = "/ga/research/company/";

    //Table options 
    public columns: ColumnOptions[];
    public source: any[];
    public elementCount: number;
    public paginationPosition = PaginationPositionDisplayEnum.UpAndDown;

    @ViewChild('table') table: TableComponent;

    constructor(
        private cdRef: ChangeDetectorRef,
        private rootComponent: AppComponent,
        private riskService: ResearchRiskRatingService,
        private clusterService: ClusterService) {
        super(cdRef);
    }

    ngOnInit() {
        this.clusterService.company.subscribe(clusters => {
            this.riskClusters = clusters;
            this.getBulkCompanies(new TableOptions("CompanyName"));
            this.setColumns();
        });
    }

    public getCompanyUrlFriendlyName(company: RiskListModel): string {
        return UtilsService.getCompanyUrlFriendlyName(company);
    }

    //new
    private setColumns() {
        this.columns = [
            new ColumnOptions('CompanyName', 'companyName', 'Company Name', 'col-md-2', true, true, false, undefined, false, true, false),
            new ColumnOptions('Exchange', 'Exchange', 'Exchange', 'col-md-2', false, true, false, undefined, true, false, false,true, undefined, false, false, true),
            new ColumnOptions('Ticker', 'Ticker', 'Ticker', 'col-md-2', false, true, false, undefined, false, false, false,true, undefined, false, false, true),
            new ColumnOptions('Assessment', 'assessment', 'ESG Risk Category', 'col-md-2', false, true, false, this.riskClusters, false, false, false,true, undefined, false, false, true),
            new ColumnOptions('SubPeerGroup', 'subPeerGroup', 'SubIndustry', 'col-md-2', false, true, false, undefined, false, false, false,true, undefined, false, false, true),
            new ColumnOptions('LastUpdate', 'LastUpdate', 'Last Update', 'col-md-2', false, true, false, undefined, true, false, false),
        ];
    }

    private getBulkCompanies(tableOptions: TableOptions): void {
        var obsGet: Observable<RiskListModel[]> = this.riskService.getCompanies(this.filter, tableOptions.pageOptions.skip, tableOptions.pageOptions.take, tableOptions.orderByCol, tableOptions.orderByAsc);
        var obsArray: any[] = [obsGet];
        if (tableOptions.pageOptions.listOnly == false) {
            let obsCount: Observable<CompanyCountModel> = this.riskService.getCompaniesCount(this.filter);
            obsArray.push(obsCount);
        }

        Observable.forkJoin(obsArray).subscribe((data) => {
            this.companies = data[0] as RiskListModel[];
            if (tableOptions.pageOptions.listOnly == false) {
                this.elementCount = (data[1] as CompanyCountModel).companyCount;
                this.table.ResetPaginationToFirstPage();
                this.rootComponent.finishLoading();
            }
            let valuesToDisplay = this.companies.map(company => {
                return {
                    company: new TableRouterLink(company.companyName, company.isEnabled, this.getCompanyUrlFriendlyName(company) ,'risk'),
                    exchange: company.exchange,
                    ticker: company.ticker,
                    assessment: company.assessment,
                    subPeerGroup: company.subPeerGroup,
                    lastUpdate: new Date(company.lastUpdate)
                };
            })
            this.source = valuesToDisplay;
        });
    }

    public tableOptionsChanged(tableOptions: TableOptions): void {
        this.getBulkCompanies(tableOptions);
    }
}